package com.training2.bookstore.product.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import com.training2.bookstore.product.model.Product;

public class ProductServiceJdbc implements ProductService {
	@Autowired DataSource ds;

	@Override
	public Collection<Product> listProduct() {
		List<Product> ps = new ArrayList<Product>();
		try {
			Connection con = ds.getConnection();
			Statement pstmt = con.createStatement();
			ResultSet rs = pstmt.executeQuery("SELECT * FROM students");
			
			while(rs.next()) {
				Product p = new Product();
				p.setDesc(rs.getString("desc"));
				p.setName(rs.getString("name"));
		
				ps.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return ps;
	}

	@Override
	public Product create(Product p) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Product update(Product p) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Product remove(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void storeProductImage(String productId, String storedFileId) {
		// TODO Auto-generated method stub

	}

	@Override
	public Collection<Product> search(String key) {
		// TODO Auto-generated method stub
		return null;
	}

}
